package com.ficus.road.maintain.third.zh.overrun;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.zh.OverloadMonitor;
import com.ficus.road.maintain.core.model.zh.OverrunCarZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.dao.OverloadMonitorDao;
import com.ficus.road.maintain.strategy.allfactory.AllFactoryContext;
import com.ficus.road.maintain.strategy.allfactory.OverrunCarStrategy;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * 2022/4/15 下午5:53
 */
@Component
public class ZHOverRunCar extends BaseZH {

    @Autowired
    OverloadMonitorDao overloadMonitorDao;
    @Autowired
    HitAllFactoryDao hitAllFactoryDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjJianCeBaseInfo";
        errMsg = "调用中海接口获取超限超载事件异常!";
        successMsg = "调用中海接口获取超限超载事件成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?")
    public void refreshOverRunCar() {
        init();
        String dateStr = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd");
        arg1 = String.format("{\"recordtype\":\"2\",\"startTime\":\"%s\",\"endTime\":\"%s\"}", dateStr, dateStr);
        String overRunCarStr = getZHData();
        if (ObjectUtils.isEmpty(overRunCarStr)) {
            return;
        }

        List<OverrunCarZH> overrunCarZHList = JSONArray.parseArray(overRunCarStr, OverrunCarZH.class);
        overrunCarZHList.parallelStream().forEach(overrunCarZH -> {
            // 更新
            String[] coordinate = overrunCarZH.getXy().split(",");
            if (ObjectUtils.isNotEmpty(coordinate) && coordinate.length == 2) {
                OverloadMonitor one = overloadMonitorDao.findOne(new Query(Criteria.where("coordinate.x").is(coordinate[0])
                        .and("coordinate.y").is(coordinate[1])));
                if (ObjectUtils.isNotEmpty(one)) {
                    one.setStatus("1");
                    overloadMonitorDao.saveOrUpdate(one);
                }
            }
        });

        arg0 = "sanwangOverrunCar";
        String overRunListStr = getZHData();
        if (ObjectUtils.isEmpty(overRunListStr)) {
            return;
        }
        AllFactoryContext allFactoryContext = new AllFactoryContext(new OverrunCarStrategy(hitAllFactoryDao));
        List<OverrunCarZH> hitList = JSONArray.parseArray(overRunListStr, OverrunCarZH.class);
        hitList.forEach(hit -> {
            if (ObjectUtils.isEmpty(hit.getUnid())) {
                return;
            }
            Hit hitOverrun = allFactoryContext.convert2AllFactoryHit(hit);
            hitAllFactoryDao.saveOrUpdate(hitOverrun);
        });


    }
}
